﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BinhSon.Dao;
using BinhSon.Services.Const;

namespace BinhSon.Services.Impl
{
    public partial class BinhSonServices
    {
        #region public methods
        public IList<LopHoc> GetListOfLopHoc(int? khoiId = null)
        {
            if (khoiId.HasValue)
            {
                return this._bsEntities.LopHoc.Where(x => x.KhoiId == khoiId.Value).ToList();
            }
            return this._bsEntities.LopHoc.ToList();
        }

        public LopHoc GetLopHocByTenLop(string tenLop)
        {
            return this._bsEntities.LopHoc.SingleOrDefault(x => x.TenLop == tenLop);
        }

        public void ThemLopHoc(LopHoc lh)
        {
            if (this.CheckExistLopHoc(lh.KhoiId, lh.TenLop))
            {
                var lopHocOld = this._bsEntities.LopHoc.SingleOrDefault(x => x.KhoiId == lh.KhoiId && x.TenLop == lh.TenLop);
                if (lopHocOld != null)
                {
                    lopHocOld.SiSoLop = lh.SiSoLop;
                    this._bsEntities.SaveChanges();
                }
            }
            else
            {
                this._bsEntities.LopHoc.Add(lh);
                this._bsEntities.SaveChanges();    
            }
        }

        public int GetSiSoLop(int lopHocId)
        {
            return this._bsEntities.HocSinh.Count(x => x.LopHocId == lopHocId);
        }

        public void CapNhatSiSoLop(int lopHocId, int siSoLop)
        {
            var entity = this._bsEntities.LopHoc.SingleOrDefault(x => x.LopHocId == lopHocId);
            if (entity == null) return;
            entity.SiSoLop = siSoLop;
            this._bsEntities.SaveChanges();
        }        

        public DataSet ExportLopHoc(int khoiId, int? lopHocId = null)
        {
            var dataSet = new DataSet();
            if (lopHocId.HasValue)
            {
                var lopHoc = this.GetLopHocByLopHoc(lopHocId.Value);
                var dataTable = this.ExportLopHocTheoLop(lopHoc);
                dataSet.Tables.Add(dataTable);
            }
            else
            {
                var dsLop = this.GetListOfLopHoc(khoiId);
                foreach (var lopHoc in dsLop)
                {
                    var dataTable = this.ExportLopHocTheoLop(lopHoc);
                    dataSet.Tables.Add(dataTable);
                }    
            }            
            return dataSet;
        }

        public LopHoc GetLopHocByLopHoc(int lopHocId)
        {
            return this._bsEntities.LopHoc.SingleOrDefault(x => x.LopHocId == lopHocId);
        }

        public IList<LopHoc> GetListOfLopHocChuaCoChuNhiem(IList<int> lopHocIds)
        {
            return this._bsEntities.LopHoc.Where(x => !lopHocIds.Contains(x.LopHocId)).ToList();
        }

        public IList<LopHoc> GetListOfLopHocChuaPhanCong(int? lopHocId = null)
        {
            //var dsLopDuocPhanCong = this._bsEntities.PhongThi.Select(x => x.LopHoc1Id).ToList();
            //dsLopDuocPhanCong.AddRange(this._bsEntities.PhongThi.Select(x => x.LopHoc2Id).ToList());

            //return lopHocId.HasValue ? this._bsEntities.LopHoc.Where(x => !dsLopDuocPhanCong.Contains(x.LopHocId) && x.LopHocId != lopHocId).ToList() : this._bsEntities.LopHoc.Where(x => !dsLopDuocPhanCong.Contains(x.LopHocId)).ToList();
            return null;
        }
        
        public bool CheckExistLopHoc(int khoiId, string tenLop)
        {
            var lopHoc = this._bsEntities.LopHoc.SingleOrDefault(x => x.KhoiId == khoiId && x.TenLop == tenLop);
            return lopHoc != null;
        }

        public bool ImportLopHocFromDataSet(DataSet dataSet)
        {
            foreach (DataTable table in dataSet.Tables)
            {                
                foreach (DataRow row in table.Rows)
                {
                    var itemArray = row.ItemArray;
                    var tenKhoi = itemArray[0].ToString();
                    var tenLop = itemArray[1].ToString().ToUpper();

                    var khoiId =
                        this._bsEntities.SystemLookup.Where(
                            x => x.LookupTypeId == (int) SystemLookupConst.Khoi && x.Description.Contains(tenKhoi))
                            .Select(x => x.LookupReference)
                            .SingleOrDefault();
                    if (khoiId == 0)
                    {
                        continue;
                    }
                    //kiem tra lop ton tai chua
                    var lopHoc = this._bsEntities.LopHoc.SingleOrDefault(x => x.KhoiId == khoiId && x.TenLop == tenLop);
                    if (lopHoc != null)
                    {
                        continue;
                    }
                    lopHoc = new LopHoc
                    {
                        KhoiId = khoiId,
                        TenLop = tenLop,
                        SiSoLop = 0
                    };
                    this._bsEntities.LopHoc.Add(lopHoc);                    
                }
            }
            this._bsEntities.SaveChanges();
            return true;
        }
        #endregion

        #region private methods
        private DataTable ExportLopHocTheoLop(LopHoc lopHoc)
        {
            var dataTable = new DataTable(lopHoc.TenLop);            
            dataTable.Columns.Add("Lớp");
            dataTable.Columns.Add("Số thứ tự", typeof(int));
            dataTable.Columns.Add("Họ và chữ lót");
            dataTable.Columns.Add("Tên");
            dataTable.Columns.Add("Giới tính");
            dataTable.Columns.Add("Ngày sinh");
            dataTable.Columns.Add("Nơi sinh");            

            var dsHocSinh = this.GetListOfHocSinhByLopHoc(lopHoc.LopHocId);
            foreach (var hocSinh in dsHocSinh)
            {
                var dataRow = dataTable.NewRow();
                var gioiTinh = hocSinh.GioiTinh ? "Nam" : "Nữ";
                dataRow[0] = lopHoc.TenLop;
                dataRow[1] = hocSinh.SoThuTu;
                dataRow[2] = hocSinh.HoVaChuLot;
                dataRow[3] = hocSinh.Ten;
                dataRow[4] = gioiTinh;
                dataRow[5] = hocSinh.NgaySinh.ToShortDateString();
                dataRow[6] = hocSinh.NoiSinh;                
                dataTable.Rows.Add(dataRow);
            }
            return dataTable;
        }       
        #endregion
    }
}
